package com.xhwl.centiotdata.handler.emergency.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.emergency.EmergencyFactory;
import com.xhwl.centiotdata.handler.emergency.EmergencyHandler;
import com.xhwl.centiotdata.mq.producer.GeneralProducer;
import com.xhwl.centiotdata.pojo.constant.HuaWeiIoTDAConstant;
import com.xhwl.centiotdata.pojo.dto.emergency.EmergencyAlarm;
import com.xhwl.centiotdata.service.cent.ICentAlarmService;
import com.xhwl.centiotdata.service.impl.DeviceServiceImpl;
import com.xhwl.common.constant.cent.alarm.AlarmConstant;
import com.xhwl.common.dto.cent.alarm.iot.IotAlarmBody;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.device.iot.DeviceServiceBody;
import com.xhwl.common.dto.cent.device.iot.IotDeviceJson;
import com.xhwl.common.pojo.cent.alarm.AlarmLog;
import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description: TODO
 * @author: chaoyun
 * @create: 2022/8/3 20:29
 **/
@Slf4j
@Service
public class EmergencyHuaWeiIoTDAHandler extends EmergencyHandler {

    private final String RESULT_TRUE = "True";
    private final String ALARM_EMRGENCY = "80300071";

    @Autowired
    private DeviceServiceImpl deviceService;
    @Autowired
    private GeneralProducer generalProducer;
    @Autowired
    private ICentAlarmService centAlarmService;

    @Override
    public void afterPropertiesSet() throws Exception {
        EmergencyFactory.register(1, this);
    }

    @Override
    public void pushEmergency(Object data, Integer projectId) {
        if (data instanceof IotDeviceJson) {
            IotDeviceJson<DeviceServiceBody<EmergencyAlarm>> requestBody;
            try {
                requestBody = (IotDeviceJson<DeviceServiceBody<EmergencyAlarm>>) data;
            } catch (Exception e) {
                log.error("pushEmergency data transform error ! {}", JSON.toJSONString(data));
                e.printStackTrace();
                return;
            }
            requestBody.getNotify_data().getBody().getServices().stream()
                    .forEach(deviceService -> {
                        AlarmLog alarmLog = new AlarmLog();
                        alarmLog.setTag(deviceService.getService_id());
                        alarmLog.setContent(JSONObject.toJSONString(deviceService));
                        alarmLog.setPushTime(new Date());
                        centAlarmService.insert(alarmLog);
                        if (HuaWeiIoTDAConstant.SERVICE_EMERGENCY_ALARM.equals(deviceService.getService_id())
                                && !RESULT_TRUE.equals(deviceService.getProperties().getCheckEmerEvent())) {
                            this.sendAlarm(projectId, ALARM_EMRGENCY, requestBody.getNotify_data().getHeader().getDevice_id(), deviceService.getEvent_time());
                        }
                    });
        } else {
            log.error("pushEmergency data format error ! {}", JSON.toJSONString(data));
            return;
        }
    }


    private void sendAlarm(Integer projectId, String alarmCode, String iotDeviceId, Date eventTime) {
        List<DeviceDTO> deviceDTOS = deviceService.findDevice(projectId, iotDeviceId);

        AlarmManagement alarmManagement = new AlarmManagement();
        alarmManagement.setProjectId(projectId);
        alarmManagement.setAlarmTime(eventTime);
        alarmManagement.setAlarmInventoryCode(alarmCode);
        alarmManagement.setDeviceId(deviceDTOS.get(0).getId());
        log.info("Emergency Alarm : {}", JSON.toJSONString(alarmManagement));
        generalProducer.send(AlarmConstant.JINMAO_TOPIC, alarmManagement);
    }
}
